package com.clkj.sunhelp.mvp.examstatuschoose;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clkj.sunhelp.R;
import com.clkj.sunhelp.adapter.list.ListAdapterForExamStatusChoose;
import com.clkj.sunhelp.app.CustomApplication;
import com.clkj.sunhelp.bean.ExamStatus;
import com.clkj.sunhelp.mvp.base.BaseDialogFragment;
import com.clkj.sunhelp.util.ToastUtil;
import com.clkj.sunhelp.util.schedulers.SchedulerProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/30.
 */

public class DialogFragmentExamStatusChoose extends BaseDialogFragment implements ExamStatusChooseContract.View{

    public static final String ARGUMENT = "argument";
    public static final String TITLE = "title";
    private String mArgument;
    private String mTitle;

    public static DialogFragmentExamStatusChoose getInstance(String argument,String title)
    {
        DialogFragmentExamStatusChoose dialogFragmentExamStatusChoose = new DialogFragmentExamStatusChoose();
        Bundle bundle = new Bundle();
        bundle.putString(ARGUMENT,argument);
        bundle.putString(TITLE,title);
        dialogFragmentExamStatusChoose.setArguments(bundle);
        return dialogFragmentExamStatusChoose;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (null!=bundle)
        {
            mArgument = bundle.getString(ARGUMENT);
            mTitle = bundle.getString(TITLE);
        }
        mPresenter = new ExamStatusChoosePresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
        setStyle(DialogFragment.STYLE_NO_TITLE, 0);
    }

    private TextView tvTip;
    private ListView lvArea;
    private RelativeLayout layoutEmpty;
    private TextView tvEmptytip;




    ExamStatusChooseContract.Presenter mPresenter;


    List<ExamStatus> mExamStatusList = new ArrayList<>();
    ListAdapterForExamStatusChoose mAdapter;

    ExamStatusChooseListener mExamStatusChooseListener;

    public ExamStatusChooseListener getExamStatusChooseListener() {
        return mExamStatusChooseListener;
    }

    public void setExamStatusChooseListener(ExamStatusChooseListener examStatusChooseListener) {
        mExamStatusChooseListener = examStatusChooseListener;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.dialog_frag_area_choose,container,false);
    }



    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        assignView(view);
        initView();
        getExamStatusList();

    }

    @Override
    public void initData() {

    }


    @Override
    public void assignView(View view) {


        tvTip = (TextView) view.findViewById(R.id.tv_tip);
        lvArea = (ListView) view.findViewById(R.id.lv_area);

        layoutEmpty = (RelativeLayout) view.findViewById(R.id.layout_empty);
        tvEmptytip = (TextView) view.findViewById(R.id.tv_emptytip);
    }

    @Override
    public void initView() {
        tvTip.setText(mTitle);
        mAdapter = new ListAdapterForExamStatusChoose(mExamStatusList,getActivity());
        lvArea.setAdapter(mAdapter);
        lvArea.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               if (null != mExamStatusChooseListener)
                {
                    mExamStatusChooseListener.onExamStatusChoose(mExamStatusList.get(position).getShname());
                }

                dismiss();
            }
        });
    }




    @Override
    public void setPresenter(ExamStatusChooseContract.Presenter presenter) {

    }

    @Override
    public void showLoading() {
        showProgressDialog(false,null);
    }

    @Override
    public void dismissLoading() {
        dismissProgressDialog();
    }

    @Override
    public void getExamStatusList() {
        mPresenter.getExamStatusList();
    }

    @Override
    public void onGetExamStatusListSuccess(List<ExamStatus> examStatusList) {
        mExamStatusList.clear();
        mExamStatusList.addAll(examStatusList);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGetExamStatusListError(String tip) {
        ToastUtil.showShort(getActivity(),tip);
        lvArea.setEmptyView(layoutEmpty);
    }

    /**
     * 省市区选择监听
     */
    public interface ExamStatusChooseListener{

        public void onExamStatusChoose(String choosedExamStatus);
    }
}

